from django.core.validators import MinValueValidator
from decimal import Decimal
from django.db import models

# Create your models here.
from django.db import models
from user.models import UserProfile, User


class FoodItem(models.Model):
    """食物项模型"""
    name = models.CharField(max_length=100, verbose_name="食物名称")
    category = models.CharField(max_length=50, choices=[
        ('breakfast', '早餐'),
        ('lunch', '午餐'),
        ('dinner', '晚餐'),
        ('snack', '零食')
    ],verbose_name="类别")
    calories = models.PositiveIntegerField(verbose_name="卡路里")
    protein = models.DecimalField(max_digits=5, decimal_places=1,verbose_name="蛋白质")  # 克
    carbs = models.DecimalField(max_digits=5, decimal_places=1,verbose_name="碳水")  # 克
    fat = models.DecimalField(max_digits=5, decimal_places=1,verbose_name="脂肪")  # 克
    fiber = models.DecimalField(max_digits=5, decimal_places=1, null=True, blank=True,verbose_name="纤维")  # 克
    suitable_for = models.CharField(max_length=20, choices=[
        ('regular', '普通'),
        ('vegetarian', '素食'),
        ('vegan', '纯素'),
        ('low_carb', '低碳水'),
        ('keto', '生酮'),
        ('gluten_free', '无麸质'),
        ('lactose_free', '无乳糖'),
    ], default='regular',verbose_name="适宜人群")
    description = models.TextField(blank=True,verbose_name="描述")

    def __str__(self):
        return f"{self.name} ({self.calories}卡)"
    class Meta:
        db_table = "tb_food_items"
        verbose_name = "食物项"
        verbose_name_plural = "食物项"


class MealPlan(models.Model):
    """用户饮食计划模型"""
    user = models.ForeignKey(UserProfile, on_delete=models.CASCADE,verbose_name="用户")
    created_at = models.DateTimeField(auto_now_add=True,verbose_name="创建时间")
    start_date = models.DateField(verbose_name="开始时间")
    end_date = models.DateField(verbose_name="结束时间")
    total_calories = models.PositiveIntegerField(verbose_name="总卡路里")
    strategy_used = models.CharField(max_length=100,verbose_name="策略使用")  # 记录使用的策略

    class Meta:
        db_table = "tb_meal_plans"
        ordering = ['-created_at']


class DailyMeal(models.Model):
    """每日饮食详情"""
    meal_plan = models.ForeignKey(MealPlan, related_name='daily_meals', on_delete=models.CASCADE,verbose_name="计划")
    date = models.DateField(verbose_name="日期")
    breakfast = models.ManyToManyField(FoodItem, related_name='breakfast_meals', blank=True,verbose_name="早餐")
    lunch = models.ManyToManyField(FoodItem, related_name='lunch_meals', blank=True,verbose_name="午餐")
    dinner = models.ManyToManyField(FoodItem, related_name='dinner_meals', blank=True,verbose_name="晚餐")
    snacks = models.ManyToManyField(FoodItem, related_name='snack_meals', blank=True,verbose_name="零食")

    class Meta:
        db_table = "tb_daily_meals"
        unique_together = ['meal_plan', 'date']


class NutritionAnalysis(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name='用户')
    net_weight = models.FloatField(verbose_name='食品净含量(g/ml)')
    image = models.CharField(max_length=500, verbose_name='营养表图片')
    nutrition_text = models.TextField(blank=True, verbose_name='识别出的营养成分')
    advice = models.TextField(blank=True, verbose_name='营养建议')
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')

    class Meta:
        db_table = 'tb_nutrition_analysis'
        verbose_name = '营养分析记录'
        verbose_name_plural = verbose_name
        ordering = ['-created_at']

    def __str__(self):
        return f"{self.user.username} - {self.created_at.strftime('%Y-%m-%d %H:%M')}"

